package com.ld.vaadin;

import com.ld.vaadin.view.IndexView;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 路由守卫（全局权限控制）
 */
public class RouteGuard extends VerticalLayout implements BeforeEnterObserver {

    @Override
    public void beforeEnter(BeforeEnterEvent event) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        // 检查是否已认证
        if (auth == null || !auth.isAuthenticated()) {
            event.rerouteTo(IndexView.class);
        }
    }
}
